package com.zsk.shop.controller.base;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.zsk.shop.common.annotion.NoPermission;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.BizException;
import com.zsk.shop.common.base.BizExceptionEnum;
import com.zsk.shop.common.constant.Constant;
import com.zsk.shop.common.enums.EnumQRCodeType;
import com.zsk.shop.common.utils.ImageUtil;
import com.zsk.shop.model.params.QRInfoParam;
import com.zsk.shop.model.results.ShopAppVersionResult;
import com.zsk.shop.service.BaseService;
import com.zsk.shop.service.ShopAppVersionService;
import com.zsk.shop.service.yulink.LinkShopLogInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@Api(tags = "基础")
@RequestMapping("/base")
public class BaseController {

    @Autowired
    private BaseService baseService;

    @Autowired
    private ShopAppVersionService appVersionService;

    @Resource
    private LinkShopLogInfoService linkShopLogInfoService;

    @NoPermission
    @ApiOperation(value = "上传图片")
    @PostMapping("/ivManagement/upload.do")
    public BaseResponse<String> upload(@RequestParam(value = "file") MultipartFile file) {
        String fileSavePath = "";
        String filename = file.getOriginalFilename();
        try {
            boolean imageFile = ImageUtil.checkFileSize(file.getSize(), 10, "M");
            if (!imageFile) {
                return BaseResponse.error("上传图片不能超过10m");
            }
            if (filename.toUpperCase().indexOf(".JPG") > 0 || filename.toUpperCase().indexOf(".PNG") > 0 || filename.toUpperCase().indexOf(".JPEG") > 0 || filename.toUpperCase().indexOf(".GIF") > 0) {
                fileSavePath = ImageUtil.ossUploadFile(file.getInputStream(), filename);
            } else {
                return BaseResponse.error("图片不符合格式");
            }
        } catch (Exception e) {
            throw new BizException(BizExceptionEnum.ERROR);
        }
        return BaseResponse.success(fileSavePath);
    }

    @ApiOperation(value = "上传各种类型文件，支持doc/ppt/xls/img/pdf")
    @PostMapping("/ivManagement/uploadFile.do")
//    @ApiImplicitParam(name = "file", value = "支持doc/ppt/xls/img/pdf")
    public BaseResponse<String> uploadFile(@RequestParam(value = "file") MultipartFile file) {
        String fileSavePath = "";
        String filename = file.getOriginalFilename();
        try {
            boolean fileSize = ImageUtil.checkFileSize(file.getSize(), 200, "M");
            if (!fileSize) {
                return BaseResponse.error("上传文件不能超过200M");
            }
            fileSavePath = ImageUtil.ossUploadFile(file.getInputStream(), filename);
        } catch (Exception e) {
            throw new BizException(BizExceptionEnum.ERROR);
        }
        return BaseResponse.success(fileSavePath);
    }

    @NoPermission
    @ApiOperation(value = "获取阿里云链接信息(STS)")
    @PostMapping("/ivManagement/createParameters.do")
    public BaseResponse createParameters() {
        try {
            return baseService.createParameters();
        } catch (ClientException e) {
            e.printStackTrace();
            return BaseResponse.error("获取失败");
        }
    }

    @NoPermission
    @ApiOperation(value = "通过类型和版本号比较是否需要升级，且给与最新版地址")
    @GetMapping("/appVersion/check.do")
    @ApiImplicitParams({@ApiImplicitParam(name = "versionCode", value = "Android版本code"),
            @ApiImplicitParam(name = "versionNumber", value = "苹果版本号"),
            @ApiImplicitParam(name = "platform", value = "1苹果, 2安卓")})
    public BaseResponse<ShopAppVersionResult> appVersionByNumberType(Long versionCode, String versionNumber, Integer platform) {
        return appVersionService.check(versionCode, versionNumber, platform);
    }

    /**
     * 阿里OSS上传文件
     *
     * @param file
     * @return
     */
    @NoPermission
    @ApiOperation(value = "阿里OSS上传文件（测试可用）")
    @PostMapping("/ivManagement/ossUpload.do")
    public BaseResponse ossUpload(@RequestParam("file") MultipartFile file,
                                  @RequestParam("accessKeyId") String accessKeyId,
                                  @RequestParam("accessKeySecret") String accessKeySecret,
                                  @RequestParam("securityToken") String securityToken) {
        return ImageUtil.ossUpload(file, accessKeyId, accessKeySecret, securityToken);
    }

    @NoPermission
    @ApiOperation(value = "生成待校验释放数据")
    @GetMapping("/checkLogInfo.do")
    public BaseResponse checkLogInfo(String phone) {
        return linkShopLogInfoService.checkLogInfo(phone);
    }

    /**
     * 二维码内容 解密
     *
     * @param str
     * @return
     */
    @NoPermission
    @ApiOperation(value = "二维码内容 解密")
    @GetMapping("/qrInfoDecode.do")
    @ApiModelProperty(name = "str", value = "需解密内容")
    public BaseResponse qrInfoDecode(String str) {
        try {
            QRInfoParam result = JSON.parseObject(BaseService.decode(str), QRInfoParam.class);
            return BaseResponse.success(result);
        } catch (Exception e) {
            return BaseResponse.error("解析失败");
        }
    }

}
